/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyClass;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author HuuTai_it
 */
public class LoadingGame extends GameCanvas implements Runnable {
    
    private Control control;
    private Image loadingBackground = Image.createImage("/MyPicture/loadingbackground.png");
    private Image loadingBar = Image.createImage("/MyPicture/loadingbar.png");
    private Image temp = Image.createImage(loadingBar, 0, 0, 1, 20, Sprite.TRANS_NONE);
    public LoadingGame(Control control) throws Exception {
        
        super(true);
        setFullScreenMode(true);
        this.control = control;
    }    
    
    public void start() {        
        
        Thread t = new Thread(this);
        t.start();        
    }
    
    public void run(){
        
        Graphics g = getGraphics(); 

        g.drawImage(loadingBackground, 0, 0, Graphics.TOP | Graphics.LEFT);        
        
        long time = System.currentTimeMillis();
        
        for(int i = 0;++i < 201;)
       {
           if(System.currentTimeMillis() - time > 50) {

                try {

                    temp = Image.createImage(loadingBar, 0, 0, i, 26, Sprite.TRANS_NONE);
                }
                catch(Exception ex){}

                g.drawImage(temp, 25, 250,  Graphics.TOP | Graphics.LEFT);
                
                g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL));
                g.setColor(0, 0, 0);
                
                if(i > 100)
                    g.drawString((i / 2) + "%", 120, 255, Graphics.TOP | Graphics.HCENTER);                

                time = System.currentTimeMillis();
           }
           
           flushGraphics();
        }
        
        control.welcome();
        control.newGame();
    }      
}
